package com.summer.safe.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.summer.safe.base.Page;
import com.summer.safe.config.annotation.NoRepeatSubmit;
import com.summer.safe.config.annotation.ResultWrapper;
import com.summer.safe.config.result.ResultCode;
import com.summer.safe.system.entity.User;
import com.summer.safe.system.service.SystemService;
import com.summer.safe.system.service.UserService;
import com.summer.safe.utils.*;
import com.summer.safe.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lm
 */
@Controller
@RequestMapping("/sys/user")
public class UserController {

    private String PREFIX = "/system/user/";

    @Autowired
    private UserService userService;
    @Autowired
    private SystemService systemService;

    @ModelAttribute("user")
    public User get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return systemService.get(id);
        } else {
            return new User();
        }
    }

    @RequiresPermissions("sys:user:view")
    @GetMapping
    public String index(User user, Model model) {
        model.addAttribute("user", user);
        return PREFIX+"userlist.html";
    }
    @RequiresPermissions("sys:user:edit")
    @GetMapping(value = "/form")
    public String form(User user, Model model) {
        model.addAttribute("user", user);
        model.addAttribute("allRoles", systemService.findAllRole());
        if(user.getOp().equals(Global.OP_VIEW)){
            return PREFIX + "userview.html";
        }
            return PREFIX + "userform.html";
    }



    @RequestMapping("/dataList")
    @ResultWrapper
    public Page testList(User user, HttpServletRequest request) {
        Page<User> page = userService.findPage(new Page(request), user);

        return page;
    }


    @PostMapping("/save")
    @ResultWrapper(ResultCode.SUCCESS_SAVE)
    public void save(User user) {
        //判断是否为演示模式
        ToolUtil.DemoMode();
        // 保存用户信息
        systemService.saveUser(user);

        // 清除当前用户缓存

    }

    @PostMapping("/delete")
    @ResultWrapper(ResultCode.SUCCESS_DELETE)
    public void delete(User user) {
        userService.delete(user);
    }






    /**
     * 跳转到个人信息修改页面
     */
    @RequestMapping("/userinfo")
    public  String userInfo( Model model) {
        User user= UserUtils.getUser();
        //处理头像路径
        String photo = StringUtils.isEmpty(user.getPhoto())?"":user.getPhoto();
        user.setPhoto(photo.replace("|",""));
        if(StringUtils.isEmpty(user.getPhoto())){
            user.setPhoto("/static/images/default_head.png");
        }
        model.addAttribute("user",user);
        return PREFIX + "userinfo.html";
    }




    /**
     * 更新个人信息
     */
    @PostMapping("/updateUser")
    @ResultWrapper(ResultCode.SUCCESS_SAVE)
    public  void updateUser( User user) {
        userService.updateUser(user);
    }

    /**
     * 更新照片
     */
    @PostMapping("/updatePhoto")
    @ResultWrapper(ResultCode.SUCCESS_SAVE)
    public  void updatePhoto(@RequestParam String photo) {
        User user= UserUtils.getUser();
        user.setPhoto(photo);
        userService.updatePhoto(user);

    }


    /**
     * 验证登录名是否有效
     *
     * @param loginName
     * @return
     */
    @PostMapping(value = "checkLoginName")
    @ResponseBody
    public JSONObject checkLoginName(String loginName) {

        if (systemService.getByLoginName(loginName)!=null) {
            return  getJson(0,"登录名已存在,请重新输入");
        } else {
            return getJson(1,"登录名已存在,请重新输入");
        }
    }

    /**
     * 修改密码
     */
    @NoRepeatSubmit
    @PostMapping("/updatePassword")
    @ResponseBody
    public JSONObject updatePassword(String password, String newPassword) {
        User user = UserUtils.getUser();
        boolean b = MD5Util.validatePassword(password, user.getPassword());
        if (b) {
            user.setPassword(MD5Util.entryptPassword(newPassword));
            userService.updatePassword(user);
            return  getJson(0,"成功");
        } else {
            return getJson(1,"密码不正确");
        }
    }
    /**
     * 验证原密码是否正确
     */

    @PostMapping(value = "/verification")
    @ResponseBody
    public JSONObject verification(@RequestParam String password) {
        User user = UserUtils.getUser();
        boolean b = MD5Util.validatePassword(password, user.getPassword());
        if (b) {
            return  getJson(0,"成功");
        } else {
            return getJson(1,"密码不正确");
        }


    }

    /**
     * 返回消息格式
     * @param code
     * @param msg
     * @return
     */
    private JSONObject getJson(Integer code,String msg){
        JSONObject json=new JSONObject();
            json.put("code", code);
            json.put("msg", msg);

            return json;

    }
    /**
     * 跳转到修改密码界面
     *
     */
    @GetMapping("/updatepasd")
    public String updatepasd() {
        return PREFIX + "password.html";
    }
    /**
     * 清除缓存
     */

    @RequestMapping("/clearCache")
    @ResultWrapper(ResultCode.CLEARCACHE)
    public  void clearCache() {
        UserUtils.clearCache();

    }





}
